package com.xxxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.dao.TeacherDao;
import com.xxxy.exception.MyException;
import com.xxxy.model.Student;
import com.xxxy.model.Teacher;
import com.xxxy.response.StudentResponse;
import com.xxxy.response.TeacherResponse;
import com.xxxy.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author sjh
 * @date 2022/1/23 12:13
 */
@Slf4j
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, Teacher> implements TeacherService{

    @Autowired
    TeacherDao teacherDao;

    @Override
    public Teacher loginByTeacher(String account, String pwd) {
        Teacher teacher =  teacherDao.selectById(account);
        if(teacher == null){
            throw new MyException("用户名密码错误");
        }
        else if(!teacher.getPwd().equals(pwd)){
            throw new MyException("用户名密码错误");
        }
        else{
            return teacher;
        }
    }

    @Override
    public TeacherResponse getByAccount(String account) {
        TeacherResponse teacher = teacherDao.getByAccount(account);
        if (teacher == null){
            throw new MyException("无该账号信息");
        }
        return teacher;
    }

    @Override
    public CommonResultPage list(Integer page, Integer size, Teacher teacher) {
        PageHelper.startPage(page,size);
        List<TeacherResponse> teacherList = teacherDao.list(teacher);
        PageInfo<TeacherResponse> pageInfo = new PageInfo<>(teacherList);
        CommonResultPage result = new CommonResultPage();
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @Override
    public List<Teacher> list(Teacher teacher) {
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(teacher != null){
            if(teacher.getAccount() != null){
                lambdaQueryWrapper.eq(Teacher::getAccount, teacher.getAccount());
            }
            if(teacher.getName() != null){
                lambdaQueryWrapper.eq(Teacher::getName, teacher.getName());
            }
            if(teacher.getSex() != null){
                lambdaQueryWrapper.eq(Teacher::getSex, teacher.getSex());
            }
        }
        List<Teacher> teacherList = teacherDao.selectList(lambdaQueryWrapper);
        return teacherList ;
    }

    @Override
    public Boolean deleteTeacher(String[] accountArray) {
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = Wrappers.lambdaQuery();
        for(String account: accountArray){
            lambdaQueryWrapper.eq(Teacher::getAccount, account);
            lambdaQueryWrapper.or();
        }
        return teacherDao.delete(lambdaQueryWrapper) == accountArray.length;
    }

    @Override
    public Boolean saveData(List<Teacher> teachers) {
        log.info("存储教师excel数据");
        boolean flag = this.saveBatch(teachers);
        if(flag){
            log.info("存储教师excel数据成功！");
            return true;
        }
        return false;
    }
}
